<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="mb-3">照片墙</div>

        <el-form :model="listQuery" inline>
          <el-form-item>
            <el-input clearable v-model="listQuery.user_no" placeholder="请输入用户编号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="float: right"
              @click="deBounceWrap(handleSearch)"
              icon="el-icon-search">搜索
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="photoData" border>
        <el-table-column label="昵称" prop="nickname"></el-table-column>
        <el-table-column label="用户编号" prop="user_no"></el-table-column>
        <el-table-column label="文件" prop="url">
          <template slot-scope="{row}">
            <el-popover v-if="handleReg(row.url)" placement="right" width="220" trigger="hover">
              <img :src="row.url" alt="" class="w-64 h-64"/>
              <img slot="reference"
                   :src="row.url"
                   preview="0"
                   :preview-text="`${row.nickname} - ${row.user_no}`"
                   alt=""
                   class="w-20 h-20"/>
            </el-popover>
            <el-popover v-else placement="top" max-width="420" trigger="hover">
              <video :src="row.url" style="max-width:200px" controls></video>
              <video slot="reference" :src="row.url" autoplay muted loop style="width: 100px;"></video>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deBounceWrap(handleDelete, scope.row)"
              plain
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page"
                      @pagination="getPhotoList"/>
    </el-card>

  </div>
</template>

<script>
import {photoList, photoDel} from '../../api/topicSquare/photoWall.js'

export default {
  name: "photoWall",
  data() {
    return {
      listQuery: {
        page: 1,
        per_page: 10,
        user_no: undefined,
      },
      photoData: [],
      total: 0,

    }
  },
  created() {
    this.getPhotoList()
  },
  methods: {
    getRandomNumber() {
      return Math.random()
    },
    getPhotoList() {
      let data = {}
      for (let key in this.listQuery) {
        if (this.listQuery[key]) {
          data[key] = this.listQuery[key]
        }
      }
      photoList(data).then(({data: {data, total}}) => {
        /* 解决图片预览数据打不开的BUG（由于图片是请求而来产生的BUG） */
        this.$previewRefresh()
        this.photoData = data
        this.total = total
      })
    },
    handleReg(url) {
      let reg = /\.jpg|\.png|\.jpeg|\.webp|\.gif$/;
      return reg.test(url);
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getPhotoList()
    },
    handleDelete(row) {
      this.$confirm("确认删除!?此步骤无法恢复。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: row.id,
          };
          photoDel(data).then((res) => {
            if (res.code === 200) {
              this.getPhotoList()
              this.$message({
                type: "success",
                message: '删除成功',
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            tyoe: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
</style>
